package copy.express.process.spit.impl;


import copy.express.pojo.Node;
import copy.express.pojo.type.Type;
import copy.express.process.spit.ConfirmTypeEndIndex;
import copy.tools.StringStrTools;

public class NumberTypeEndInex implements ConfirmTypeEndIndex {

    private static NumberTypeEndInex numberTypeEndInex = new NumberTypeEndInex();

    private NumberTypeEndInex() {
    }
    public static NumberTypeEndInex getInstance(){
        return numberTypeEndInex;
    }

    @Override
    public Node createNode(int start, int end, char[] chars) {
        String str = StringStrTools.appendFromStartToEnd(start, end - 1, chars);
        //小数的默认类型: double处理
        if (str.contains(".")){
            return new Node(str, Type.DoubleNumberType.DOUBLE);
        }
        //整数的默认类型: int处理
        return new Node(str,Type.IntNumberType.INT);
    }

    @Override
    public boolean confirmType(char c) {
        if (c >= '0' && c <= '9'){
            return true;
        }
        if (c == '.'){
            return true;
        }
        return false;
    }

    @Override
    public int findEndIndex(int start, int pos, char[] chars) {
        if (pos==chars.length){
            return pos;
        }
        if ( chars[pos] == ' ' || chars[pos] == ','  )return pos;
        if  ( confirmType(chars[pos]) ){

            return findEndIndex(start,pos+1,chars);
        }else{
            return pos;
        }
    }
}
